package enumpackage;

/**
 * @author cyt
 * @create 2020-09-09 16:13
 */
public class TestOne {
    /**
     * 这是因为enum类型的每个常量在JVM中只有一个唯一实例，所以可以直接用==比较
     *
     * @param args
     */
    public static void main(String[] args) {
        Weekday sat = Weekday.SAT;
        Weekday sun = Weekday.SUN;
        System.out.println(sat == Weekday.SAT);

        String name = Weekday.SUN.name();
        int ordinal = Weekday.SAT.ordinal();

        Weekday[] values = Weekday.values();

        for (Weekday value : values) {
            System.out.println(value.name());
        }

        System.out.println(Response.SUCCESS.getCode());
        System.out.println(Response.ERROR.getCode());


        Response error = Response.valueOf("ERROR");
        System.out.println(error.getCode());

        Response[] responses = Response.values();

        for (Response response : responses) {
            System.out.println(response.getMsg() + "- > " + response.getCode());
        }
    }
}

enum Weekday {
    SUN, MON, TUE, WED, THU, FRI, SAT;
}

/**
 * 定义的enum类型总是继承自java.lang.Enum，且无法被继承；
 * 只能定义出enum的实例，而无法通过new操作符创建enum的实例；
 * 定义的每个实例都是引用类型的唯一实例；
 * 可以将enum类型用于switch语句。
 **/
enum Response {
    SUCCESS(2000, "SUCCESS"),
    ERROR(2001, "ERROR");
    private Integer code;
    private String msg;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}
